﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Brain.Rendering
{
    public class GridAnim_Pinch : IGridAnimation
    {
        public PostProcessor_CPUGrid Grid { get; set; }

        public bool ShouldUpdate { get; set; }

        public GridAnim_Pinch()
        {
        }

        public virtual void ApplyPinch(Vector2 center, float intensity)
        {
            Vector2 gridSize = new Vector2(this.Grid.Colums, this.Grid.Rows) * Grid.CellSize;

            for (int i = 0; i < Grid.Vertices.Length; i++)
            {
                //Get the position in pixels
                Vector2 pos = Grid.GetUV0(i) * gridSize;

                //Get the direction from the current grid vertex to the center-of-pinch
                Vector2 dir = pos - center;
                float length = dir.Length();
                float minDisplacement = -length;
                if (dir.Length() != 0)
                {
                    dir.Normalize();
                }

                //Compute the displacement, but not lower than the minDisplacement
                Vector2 displacement = dir * Math.Max(intensity, minDisplacement);
                //Find the new position
                Vector2 newPos = pos + displacement;
                //Set the new Texture Coordinates
                Grid.SetUV0(i, newPos / gridSize);
            }
        }

        public void Update()
        {
        }
    }
}
